package org.lex.checksum.utils;

public class HexEncoder {
	private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', };

	public static String toHex(byte[] data) {
		char[] buf = new char[data.length * 2];
		for (int i = 0; i < data.length; i++) {
			byte2hex(0xFF & data[i], buf, 2 * i);
		}
		return new String(buf);
	}

	private static void byte2hex(int b, char[] hex, int offset) {
		// high 4 bit
		hex[offset] = HEX_DIGITS[(b & 0xf0) >>> 4];
		// low 4 bit
		hex[offset + 1] = HEX_DIGITS[b & 0xf];
	}
}
